<?php
class order extends serviceAdminBase {
    /** @var  OrderMode */
    protected $orderMode;
    function __construct() {
        parent::__construct();
        $this->cms->setPageTitle("订单管理");
        $this->cms->setControlFile("service/order/order_control.json");
        $this->orderMode=OrderMode::init();
    }

    function indexTask($page=1,$state=null,$orderId,$username){
        $this->cms->setActionTitle("所有订单");
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        $user_info=$this->user->getLoginInfo();
        $service=ServiceMode::init();
        $service_id=$service->getServiceId($user_info['id']);
        $order=OrderMode::init();
        $username=isset($username)?trim($username):null;
        if(isset($username)){
            $user_id = UserModule::init('user')->getIdByUsername($username);
        }else{
            $user_id = null;
        }
        if(isset($orderId)){
            $r['order_info'][0]=$order->getDetailWithoutJoin($orderId);
            $order_total=1;
        }else{
            list($r['order_info'],$order_total)=$order->getList($user_id,$shop_id = null,$state,$visit_state=null,
                $byDay = null,$type = null,$allocate = null,$service_id,$countOfPage ,$offset);
        }
        $url=WebRouter::init()->getQuestion("state={$state}&orderId={$orderId}&username={$username}&page=");
        $r['pages']=new Page($page,$order_total,$countOfPage);
        $r['pages']->setPageCallback(create_function('$page','return "'.$url.'".$page;'));
        $r['currentState']=$state;
        $r['orderState']=array(
            'unpaid'=>"未付款",'unshipped'=>"未发货",'shipped'=>"已发货",'shipped_direct'=>"已向用户发货",
            'service'=>"等待服务站配送",'received'=>"用户已签收",'confirmed'=>"用户已确认",'refunding'=>"进入退货流程",
            'evaluate'=>"已评价",'unused'=>"未使用",'used'=>"已使用");
        $this->cms->tableScene($r,"service/order/order.php");
    }

    //以下是一系列服务站处理的内容

    function dealServiceReceiveTask($order_id){
        $orderInfo=$this->orderMode->getDetail($order_id);
        if(empty($orderInfo)) return new Intent(404,"未找到该订单");
        $order_state=$this->orderMode->setState($order_id,'service');
        if($order_state){
            $r['url']=WebRouter::init()->getAction("index");
            $r['message']="操作成功";
        }else{
            $r['message']="操作失败";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

    function dealReceivedTimeTask($order_id){
        $orderInfo=$this->orderMode->getDetail($order_id);
        if(empty($orderInfo)) return new Intent(404,"未找到该订单");
        $order_state=$this->orderMode->setState($order_id,'received');
        if($order_state){
            $r['url']=WebRouter::init()->getAction("index");
            $r['message']="操作成功";
        }else{
            $r['message']="操作失败";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

    function detailTask($order_id){
        $this->cms->setActionTitle("订单基本信息");
        $order=OrderMode::init();
        $order_info=$order->getDetail($order_id);
        $ad=AddressMode::init();
        $userAddId=$order_info['order']['user_address_id'];
        if(!empty($userAddId)){
            $r['userAddress']=$ad->getUserAddressAsString($userAddId);
        }
        $user=UserMode::init();
        $r['user_info']=$user->getDetail($order_info['order']['user_id']);
        $r['user_phone']=UserModule::init('user')->getDetail($order_info['order']['user_id'])['username'];
        $r['order']=$order_info['order'];
        $r['goods_in_order']=$order->getGoodsInOrder($r['order']['id']);
        $r['goods_in_order']=is_array($r['goods_in_order'])?$r['goods_in_order']:array();
        $r['shop']=$order_info['shop'];
        $service=ServiceMode::init();
        $service_id=$r['order']['service_id'];
        $r['service_info']=$service->getDetail($service_id);
        $r['current_type']=$r['order']['type'];
        $r['shopType']=array("normal"=>"普通商品","commodity"=>"大宗商品","service"=>"服务商品","non_sale"=>"广告商品");
        $r['current_state']=$r['order']['state'];
        $r['orderState']=array(
            'unpaid'=>"未付款",'unshipped'=>"未发货",'shipped'=>"已发货",'shipped_direct'=>"已向用户发货",
            'service'=>"等待服务站配送",'received'=>"用户已签收",'confirmed'=>"用户已确认",'refunding'=>"进入退货流程",
            'evaluate'=>"已评价",'assigned'=>"其他");
        $r['buyer_phone']=UserModule::init('user')->getDetail($r['order']['buyer_id'])['username'];
        $r['buyer_name']=UserMode::init()->getDetail($r['order']['buyer_id'])['real_name'];
        $this->cms->tableScene($r,"service/order/orderDetail.php");
    }

    // 提醒商家尽快发货
    function dealUrgeShopTask($order_id){
        $msgMode=MsgMode::init();
        $orderInfo=$this->orderMode->getDetail($order_id);
        $serviceId=$this->serviceInfo['id'];
        $user_info=UserMode::init()->getDetail($orderInfo['order']['user_id']);
        if(empty($orderInfo)|| $orderInfo['order']['service_id']!=$serviceId) return new Intent(404,"未找到该订单");
        $shopUserId=ShopMode::init()->getUserId($orderInfo['order']['shop_id']);
        $orderInfo['goods']=is_array($orderInfo['goods'])?$orderInfo['goods']:array();
        foreach($orderInfo['goods'] as $v){
            if(!empty($shopUserId)){
                $msgMode->send($shopUserId,"{$this->serviceInfo['service_name']}提醒您尽快发货",
                    "{$user_info['real_name']}客户有一个订单还滞留在您的仓库，订单号{$orderInfo['order']['id']},商品名{$v['name']},请您尽快发货，以便您的客户得到更好的服务");
                $r['message']="提醒已经发送给商家";
            }else{
                $r['message']="发送失败";
            }
            View::displayAsHtml($r,"plugin/state/tips.php");
        }
    }
}